import { request } from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 测试报告接口
export interface TestReport {
  id: string
  name: string
  test_plan_id: string
  version_id?: string
  file_path?: string
  report_data?: any
  creator_id: string
  generated_at: string
  created_at: string
  updated_at: string
}

export interface TestReportCreate {
  name: string
  test_plan_id: string
  version_id?: string
  file_path?: string
  report_data?: any
  creator_id?: string
}

export interface TestReportUpdate {
  name?: string
  version_id?: string
  file_path?: string
  report_data?: any
}

export interface TestReportListResponse extends TestReport {
  test_plan_name?: string
  version_name?: string
  version_number?: string
  creator_name?: string
}

export interface TestReportDetailResponse extends TestReport {
  test_plan_name?: string
  version_name?: string
  version_number?: string
  creator_name?: string
}

export interface TestReportLinkVersionRequest {
  version_id: string
}

export interface TestReportUploadRequest {
  file_path: string
}

// 分页列表响应类型
export interface PaginatedListResponse<T> {
  items: T[]
  total: number
  page: number
  size: number
}

// API函数
export function getTestReports(params?: {
  test_plan_id?: string
  version_id?: string
  page?: number
  size?: number
}): Promise<PaginatedListResponse<TestReportListResponse>> {
  const requestParams: any = {}
  if (params?.test_plan_id) requestParams.test_plan_id = params.test_plan_id
  if (params?.version_id) requestParams.version_id = params.version_id
  if (params?.page) requestParams.skip = (params.page - 1) * (params.size || 20)
  if (params?.size) requestParams.limit = params.size

  return request.get('/test-reports', { params: requestParams }).then(response => {
    if (response.data && response.data.success) {
      const data = response.data.data
      const pagination = response.data.pagination
      if (Array.isArray(data) && pagination) {
        return {
          items: data,
          total: pagination.total || 0,
          page: pagination.page || 1,
          size: pagination.size || (params?.size || 20)
        }
      }
    }
    return {
      items: [],
      total: 0,
      page: params?.page || 1,
      size: params?.size || 20
    }
  })
}

export function getTestReportById(id: string): Promise<ApiResponse<TestReportDetailResponse>> {
  return request.get(`/test-reports/${id}`).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '获取测试报告失败')
  })
}

export function createTestReport(data: TestReportCreate): Promise<ApiResponse<TestReport>> {
  return request.post('/test-reports', data).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '创建测试报告失败')
  })
}

export function updateTestReport(id: string, data: TestReportUpdate): Promise<ApiResponse<TestReport>> {
  return request.put(`/test-reports/${id}`, data).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '更新测试报告失败')
  })
}

export function deleteTestReport(id: string): Promise<ApiResponse<null>> {
  return request.delete(`/test-reports/${id}`).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '删除测试报告失败')
  })
}

export function linkVersion(
  testReportId: string,
  data: TestReportLinkVersionRequest
): Promise<ApiResponse<TestReport>> {
  return request.post(`/test-reports/${testReportId}/link-version`, data).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '关联版本失败')
  })
}

export function uploadReportFile(
  testReportId: string,
  data: TestReportUploadRequest
): Promise<ApiResponse<TestReport>> {
  return request.post(`/test-reports/${testReportId}/upload`, data).then(response => {
    if (response.data && response.data.success) {
      return response.data
    }
    throw new Error(response.data?.message || '上传报告文件失败')
  })
}

